from qiniu import Auth, put_file, etag,put_data
import qiniu.config

def image_storage(localfile):
    #需要填写你的 Access Key 和 Secret Key
    access_key = 'ygL5ApbNr3ySsaUY6kbNZGDSOq7IuTJgFL0ygnT6'
    secret_key = 'UMGrR02vCxDBQtc6jDqKIJaudgxY0l4ZIKO-qZ0E'
    #构建鉴权对象
    q = Auth(access_key, secret_key)
    #要上传的空间
    bucket_name = 'hg-flask-project'
    #上传到七牛后保存的文件名
    key = None
    #生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)
    #要上传文件的本地路径
    # localfile = "./55.jpg"
    # ret, info = put_file(token, key, localfile)  #这个是上传文件到七牛云
    ret, info = put_data(token, key, localfile)  #这个是上传二进制流到七牛云

    if info.status_code == 200:
        return ret.get("key")
    else:
        return ""


if __name__ == '__main__':
    f = open("./55.jpg","rb")
    locafile1 = f.read()
    image_name = image_storage(localfile=locafile1)
    print(image_name)
    f.close()
